package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{

    static {
        System.out.println("---------loading---------");
    }

    //创建集合,充当学生对象的容器
    private static ArrayList<Student> sturr = new ArrayList<>();

    //增添方法
    @Override
    public boolean addStudent(Student stu) {

        sturr.add(stu);

        return true;
    }

    //查看方法
    @Override
    public Student[] findAllStudent() {

        //创建数组
        Student[] stusz = new Student[sturr.size()];

        //遍历集合,获取集合中每个对象索引
        for (int i = 0; i < sturr.size(); i++) {

            //将集合中的对象复制到数组中
            stusz[i] = sturr.get(i);
        }
        return stusz;
    }

    //修改方法
    @Override
    public void updateStudent(String updateId, Student stu) {

        sturr.set(getIndex(updateId),stu);
    }

    //删除方法
    @Override
    public boolean deleteStudent(String delId) {

        int index = getIndex(delId);
        if (index == -1) {
            return false;
        } else {
            sturr.remove(index);
            return true;
        }
    }

    //根据学号找到对应对象索引的方法
    @Override
    public int getIndex(String updateId) {

        //用标记为思想假设修改的学号在数组中不存在;
        int index = -1;

        for (int i = 0; i < sturr.size(); i++) {

            if (updateId.equals(sturr.get(i).getId())) {

                index = i;

                break;
            }
        }
        return index;
    }
}
